import util from '@/libs/util';
import Cookies from 'js-cookie';

export default {
    name: 'exportable-table',
    props: {
        ids: ''
    },
    data () {
        return {
            columns: [{
                type: 'selection',
                width: 60,
                align: 'center'
            }, {
                title: '用户名称',
                key: 'realname'
            }, {
                title: '联系电话',
                key: 'phone'
            }, {
                title: '性别',
                key: 'sex',
                render: (h, params) => {
                    return h('div', {}, (params.row.sex === 1 ? '男' : '女'));
                }
            }, {
                title: '年龄',
                key: 'age'
            }, {
                title: '学校',
                key: 'school'
            }],
            baseuser: JSON.parse(Cookies.get('baseUser')),
            data: [],
            query: {
                realname: '',
                phone: ''
            },

            total: 0,
            pageIndex: 1,
            pageSize: 100,
            memberIds: [],
            rowIds: []
        };
    },
    methods: {
        loadList (ids) {
            util.ajax.post('/busine/member/list', util.toFormData({
                page: this.pageIndex,
                size: this.pageSize,
                realname: this.query.realname,
                phone: this.query.phone,
                orgid: this.baseuser.orgId
            })).then(res => {
                this.data = res.data.data.list;
                this.total = res.data.data.total;
                this.rowIds = [];
                if (ids) {
                    this.memberIds = ids;
                    this.data.forEach(item => {
                        if (this.memberIds.indexOf(item.id) > -1) {
                            item._checked = true;
                            this.rowIds.push(item.id);
                        }
                    });
                }
            });
        },
        refresh () {
            this.query = {
                realname: '',
                phone: ''
            };
            this.loadList();
        },
        onselectionchange (selection) {
            var addIds = [];
            var delIds = [];
            var selectIds = [];
            selection.forEach(item => {
                selectIds.push(item.id);
                if (this.rowIds.indexOf(item.id) === -1) {
                    addIds.push(item.id);
                    this.rowIds.push(item.id);
                }
            });
            this.rowIds.forEach(item => {
                if (selectIds.indexOf(item) === -1) {
                    delIds.push(item);
                }
            });
            addIds.forEach(item => {
                this.memberIds.push(item);
            });
            delIds.forEach(item => {
                if (this.memberIds.indexOf(item) > -1) {
                    this.memberIds.splice(this.memberIds.indexOf(item), 1);
                }
            })
            this.$emit('onSelect', this.memberIds);
        }
    }
}